﻿<%@ Page Language="C#" MasterPageFile="~/Manager/Admin/Admin.master" Title="Filter Builder" %>
<script runat="server">
	public string SQL { get { return HttpContext.Current.Request["SQL"] + ""; } }
	public string FilterString { get { return HttpContext.Current.Request["FilterString"] + ""; } }
	public GridFilterDS.FilterBuilderDataTable FilterTable {
		get { return !IsPostBack ? null : Session["FilterTable"] as GridFilterDS.FilterBuilderDataTable; }
		set { Session["FilterTable"] = value; }
	}
	protected override void OnInit(EventArgs e) {
		base.OnInit(e);
		dsFilter.Selected += new ObjectDataSourceStatusEventHandler(dsFilter_Selected);
		dsColumns.EnableCaching = IsPostBack;
		JS.ScriptLocalIDs(Page, txtFilter,btnClear);
	}

	protected void Value1_DataBinding(object sender, EventArgs e) {
		TextBox tb = (TextBox)sender;
		GridViewRow drv = tb.NamingContainer as GridViewRow;
		if (drv == null) return;
		TextBoxWatermarkExtender wm = tb.Parent.FindControl("wm1") as TextBoxWatermarkExtender;
		DataRow row = ((DataRowView)drv.DataItem).Row;
		string operand = row["Operand"].ToString().ToUpper();
		switch (operand) {
			case "NOT IN":
			case "IN":
				tb.TextMode = TextBoxMode.MultiLine;
				tb.Style[HtmlTextWriterStyle.Overflow] = "visble";
				//tb.Style[HtmlTextWriterStyle.WhiteSpace] = "pre";
				break;
		}
		wm.WatermarkText = FilterBuilderDataSource.GetOperads().Select("Value='"+operand+"'")[0]["Desc1"] + "";
		wm.Enabled = wm.WatermarkText != "";
		HtmlGenericControl error= tb.Parent.FindControl("Error") as HtmlGenericControl;
		if( row["Value1"] + "" != "" )
		switch (row["FieldType"] + "") {
			case "DateTime":
				DateTime dtTry;
				if( !DateTime.TryParse(row["Value1"] + "",out dtTry) )
					error.InnerHtml = "Value must be in date format: mm/dd/yyyy.";
				break;
			case "Number":
				double dTry;
				if (!double.TryParse(row["Value1"] + "",out dTry))
					error.InnerHtml = "Value must be a number.";
				break;
		}
	}
	protected void Value2_DataBinding(object sender, EventArgs e) {
		TextBox tb = (TextBox)sender;
		GridViewRow drv = tb.NamingContainer as GridViewRow;
		if (drv == null) return;
		DataRow row = ((DataRowView)drv.DataItem).Row;
		switch (row["Operand"].ToString().ToUpper()) {
			case "NOT BETWEEN":
			case "BETWEEN":
				tb.Style["display"] = "";
				break;
			default:
				tb.Text = ""; break;
		}
		//tb.Style["display"] = ((DataRowView)drv.DataItem).Row["Operand"].ToString() == "BETWEEN" ? "" : "none";
	}

	void dsFilter_Selected(object sender, ObjectDataSourceStatusEventArgs e) {
		FilterTable = (GridFilterDS.FilterBuilderDataTable)e.ReturnValue;
		txtFilter.Value = Data.DataSources.FilterBuilderDataSource.ParseFilterTable(SQL, (GridFilterDS.FilterBuilderDataTable)e.ReturnValue);
	}

	int GetFilterID(object sender) {
		System.Web.UI.WebControls.DataControlFieldCell cell = ((Control)sender).Parent as System.Web.UI.WebControls.DataControlFieldCell;
		System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)cell.Parent;
		return int.Parse(row.Cells[1].Text);
	}
	protected void Value_SelectedIndexChanged(object sender, EventArgs e) {
		ITextControl tc = (ITextControl)sender;
		WebControl wc = (WebControl)sender;
		FilterBuilderDataSource.UpdateEx(SQL,GetFilterID(sender), wc.ID, tc.Text, FilterTable);
	}
	protected void Operand_SelectedIndexChanged(object sender, EventArgs e) {
		ListControl ddl = (System.Web.UI.WebControls.ListControl)sender;
		ddl.ToolTip = ddl.SelectedValue;
		FilterBuilderDataSource.UpdateEx(SQL,GetFilterID(sender), ddl.ID, ddl.SelectedValue, FilterTable);
	}

	protected void btnClear_Click(object sender, EventArgs e) {
		FilterBuilderDataSource.Clear(FilterTable);
	}
</script>
<asp:Content ID="C" ContentPlaceHolderID="AC" Runat="Server">
<link href="/CSS/Outlook.css" rel="stylesheet" type="text/css" />
<style>
TABLE.GridView TD INPUT,TEXTAREA
{
	border:1px solid gainsboro;
	font-weight:bold;
	Font-Names="monospace";
	vertical-align:middle;
	width:20ex;
}
TABLE.GridView TD SELECT
{ font-size:125%;
  font-family:Monospace;
}
.wm{color:LightGrey}
</style>
<div id="Error" runat="server" />
<asp:UpdatePanel runat="server"><ContentTemplate>
<UI:GridViewEx ID="grv" DataSourceID="dsFilter" DataKeyNames="ID" AllowEdit="false" AutoGenerateColumns="false" runat="server" RowStyle-VerticalAlign="Top">
<Columns>
	<asp:BoundField DataField="ID" HeaderText="ID" />
	<asp:TemplateField HeaderText="Field Name">
		<ItemTemplate>
			<asp:DropDownList ID="FieldName" SelectedValue='<%#Eval("FieldName")%>' DataSourceID="dsColumns" DataTextField="Name" DataValueField="Value" AutoPostBack="true" OnSelectedIndexChanged="Operand_SelectedIndexChanged" runat="server" onclick="event.cancelBubble=true" />
		</ItemTemplate>
	</asp:TemplateField>
	<asp:TemplateField HeaderText="Operand">
		<ItemTemplate>
			<asp:DropDownList ID="Operand" SelectedValue='<%#Eval("Operand")%>' DataSourceID="dsOperands" DataTextField="Name" DataValueField="Value" AutoPostBack="true" runat="server"
					 OnSelectedIndexChanged="Operand_SelectedIndexChanged" onclick="event.cancelBubble=true" ToolTip="Dimok"
			></asp:DropDownList>
		</ItemTemplate>
	</asp:TemplateField>
	<asp:TemplateField HeaderText="Value(s)">
		<ItemTemplate>
			<ajaxtk:TextBoxWatermarkExtender ID="wm1" TargetControlID="Value1" WatermarkText="value" WatermarkCssClass="wm" runat="server" />
			<asp:TextBox ID="Value1" OnDataBinding="Value1_DataBinding" Text='<%#Eval("Value1")%>' OnTextChanged="Value_SelectedIndexChanged" AutoPostBack="true" onclick="event.cancelBubble=true" runat="server" />
			<asp:Literal ID="ltrAnd" Text=" AND " runat="server" Visible='<%#Eval("Operand","{0}").Contains("BETWEEN")%>' />
			<ajaxtk:TextBoxWatermarkExtender ID="wm2" TargetControlID="Value2" WatermarkText="CEILING" WatermarkCssClass="wm" runat="server" />
			<asp:TextBox ID="Value2" OnDataBinding="Value2_DataBinding" Text='<%#Eval("Value2")%>' OnTextChanged="Value_SelectedIndexChanged" AutoPostBack="true" onclick="event.cancelBubble=true" style="display:none" runat="server" />
			<div ID="Error" runat="server" style="color:Maroon" />
		</ItemTemplate>
	</asp:TemplateField>
</Columns>
</UI:GridViewEx>
<input ID="txtFilter" style="overflow:visible" runat="server" />
<asp:Button ID="btnClear" runat="server" OnClick="btnClear_Click" style="display:none" />
</ContentTemplate></asp:UpdatePanel>
<table CellPadding="0" CellSpacing="5">
<tr>
	<td Align="center"><input type="button" style="white-space:pre" value="    OK    " onclick="saveFilter()" /></td>
	<td Align="center"><input type="button" value="Cancel" onclick="window.close()" /></td>
	<td Align="center"><input type="button" value="  Clear  " onclick="btnClear().click()" /></td>
</tr></table>
<ds:FilterBuilderDataSource ID="dsFilter" SelectMethod="GetFilter" runat="server">
	<SelectParameters>
		<asp:ControlParameter ControlID="dsFilter" PropertyName="Page.SQL" Name="SQL" Type="String" />
		<asp:ControlParameter ControlID="dsFilter" PropertyName="Page.FilterString" Name="FilterString" Type="String" />
		<asp:ControlParameter ControlID="dsFilter" PropertyName="Page.FilterTable" Name="FilterTable" />
	</SelectParameters>
	<DeleteParameters>
		<asp:ControlParameter ControlID="dsFilter" PropertyName="Page.FilterTable" Name="FilterTable" Type="Object" />
	</DeleteParameters>
</ds:FilterBuilderDataSource>
<ds:FilterBuilderDataSource CacheDuration="Infinite" ID="dsColumns" SelectMethod="GetColumns" runat="server">
	<SelectParameters>
		<asp:ControlParameter ControlID="dsColumns" PropertyName="Page.SQL" Name="sql" Type="Object" />
	</SelectParameters>
</ds:FilterBuilderDataSource>
<ds:FilterBuilderDataSource ID="dsOperands" SelectMethod="GetOperads" runat="server" />
<script>
function saveFilter(){
	dialogArguments.callBack(txtFilter().value);
	window.close();
}
</script>
</asp:Content>

